//防止连续点击多次打开页面
let navigateToCount = 0;
export function navigateTo(url) {
	if (navigateToCount == 0) {
		navigateToCount = 1;
		uni.navigateTo({
			url: url
		});
		setTimeout(function() {
			navigateToCount = 0;
		}, 1000);
	}
}
// 页面 返回
export function backs(url) {
	setTimeout(function() {
		uni.navigateBack()
	}, 1000);
}

//  去登陆
export function toLogin() {
	let url = 'eper://weichat.xar.com.login'
	plus.runtime.openURL(url, res => {});
}

// 检查 有没有 token
export function checkingLogin() {
	let token = uni.getStorageSync('token')
	if(!token){
		return new Promise((resolve, errs) => {  
			uni.sendNativeEvent('infoUser', {
				msg: 'unimp message!!!'
			}, ret => {
				ret = JSON.parse(ret)
				if (ret.token) {
					let tokens = ret.token;
					uni.setStorageSync('token', tokens)
					resolve(true)
				}else{
					resolve(false)
				} 
			},err=>{
				resolve(false)
			}) 
			setTimeout(()=>{
				resolve(false)
			},400)
		})
	}else{
		return true
	} 
} 

// 打开地图
export function mapServe(latitude, longitude, address) {
	let name = address
	let url = "";
	if (plus.os.name == "Android") { //判断是安卓端
		plus.nativeUI.actionSheet({ //选择菜单
			title: "选择地图应用",
			cancel: "取消",
			buttons: [{
				title: "腾讯地图"
			}, {
				title: "百度地图"
			}, {
				title: "高德地图"
			}]
		}, function(e) {
			switch (e.index) {
				//下面是拼接url,不同系统以及不同地图都有不同的拼接字段
				case 1:
					//注意referer=xxx的xxx替换成你在腾讯地图开发平台申请的key
					url = `qqmap://map/geocoder?coord=${latitude},${longitude}&referer=xxx`;
					break;
				case 2:
					url =
						`baidumap://map/marker?location=${latitude},${longitude}&title=${name}&coord_type=gcj02&src=andr.baidu.openAPIdemo`;
					break;
				case 3:
					url =
						`androidamap://viewMap?sourceApplication=appname&poiname=${name}&lat=${latitude}&lon=${longitude}&dev=0`;
					break;
				default:
					break;
			}
			if (url != "") {
				url = encodeURI(url);
				//plus.runtime.openURL(url,function(e){})调起手机APP应用
				plus.runtime.openURL(url, function(e) {
					plus.nativeUI.alert("本机未安装指定的地图应用");
				});
			}
		})
	} else {
		// iOS上获取本机是否安装了百度高德地图，需要在manifest里配置
		// 在manifest.json文件app-plus->distribute->apple->urlschemewhitelist节点下添加
		//（如urlschemewhitelist:["iosamap","baidumap"]）  
		plus.nativeUI.actionSheet({
			title: "选择地图应用",
			cancel: "取消",
			buttons: [{
				title: "腾讯地图"
			}, {
				title: "百度地图"
			}, {
				title: "高德地图"
			}]
		}, function(e) {
			switch (e.index) {
				case 1:
					url = `qqmap://map/geocoder?coord=${latitude},${longitude}&referer=xxx`;
					break;
				case 2:
					url =
						`baidumap://map/marker?location=${latitude},${longitude}&title=${name}&content=${name}&src=ios.baidu.openAPIdemo&coord_type=gcj02`;
					break;
				case 3:
					url =
						`iosamap://viewMap?sourceApplication=applicationName&poiname=${name}&lat=${latitude}&lon=${longitude}&dev=0`;
					break;
				default:
					break;
			}
			if (url != "") {
				url = encodeURI(url);
				plus.runtime.openURL(url, function(e) {
					plus.nativeUI.alert("本机未安装指定的地图应用");
				});
			}
		})
	}
}
